package com.cwt_taxi_xa.timer;

import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.protocol.DispatchCenterUsers;
import com.cwt_taxi_xa.util.AppLog;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimerCommonReTime extends TimerTask {

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Override
	public void run() {

		/**
		 * 刷新顶层界面时间
		 */
		if (MainApplication.getInstance().topActivity != null) {
			MainApplication.getInstance().topActivity.sendMsg(1002, sdf.format(new Date()));
		}

		if(TimerCommon.lastReTime!=0&&System.currentTimeMillis()-TimerCommon.lastReTime>10000){
			try {
				AppLog.savelog("TimerCommonReTime检测到通用线程停止，重新启动---1");
				TimerCommon.lastReTime = System.currentTimeMillis();
				MainApplication.getInstance().mtimer.cancel();
				MainApplication.getInstance().mtimer = null;
				MainApplication.getInstance().mTimerCom.cancel();
				MainApplication.getInstance().mTimerCom = null;
				new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							Thread.sleep(2000);
						} catch (Exception e) {
						}
						MainApplication.getInstance().mTimerCom = new TimerCommon();
						MainApplication.getInstance().mtimer = new Timer();
						MainApplication.getInstance().mtimer.schedule(MainApplication.getInstance().mTimerCom, 1000, 1000);
					}
				}).start();
				AppLog.savelog("TimerCommonReTime检测到通用线程停止，重新启动---1");
			} catch (Exception e) {
			}
		}



//		AppLog.savelog("TimerHeartBeat.lastReTime:"+TimerHeartBeat.lastReTime);
		if(System.currentTimeMillis()-TimerHeartBeat.lastReTime>10000){
			try {
				// 启动心跳维持连接
				AppLog.savelog("TimerCommonReTime检测到心跳线程停止，重新启动---1");
				TimerHeartBeat.lastReTime = System.currentTimeMillis();
				DispatchCenterUsers.startHeartBeat();
				AppLog.savelog("TimerCommonReTime检测到心跳线程停止，重新启动---2");
			} catch (Exception e) {
			}
		}

		 
	}
	
	
}
